package com.iemes.controller.equip;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.controller.index.BaseController;
import com.iemes.entity.EquipCategoryFormMap;
import com.iemes.entity.EquipFileFormMap;
import com.iemes.entity.MainMouldFileFormMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.RepairappFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipCategoryMapper;
import com.iemes.mapper.equip.EquipFileMapper;
import com.iemes.mapper.equip.RepairappMapper;
import com.iemes.mapper.hr.StaffMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/maintain/")
@Controller
public class MaintainController extends BaseController{
	@Inject
	private RepairappMapper repairappMapper;
	@Inject
	private EquipCategoryMapper equipCategoryMapper;
	@Inject
	private EquipFileMapper equipFileMapper;
	@Inject
	private StaffMapper staffMapper;
	@RequestMapping("examine")
	public String examine(Model model){
		return Common.BACKGROUND_PATH + "/maintain/maintain_list";
	}
	
	@RequestMapping("maintain_info")
	public String maintain_info(Model model){
		//获取所有设备种类
		EquipCategoryFormMap equipCategoryFormMap=getFormMap(EquipCategoryFormMap.class);
		List<EquipCategoryFormMap> equipCategoryFormMaps=equipCategoryMapper.findByWhere(equipCategoryFormMap);
		model.addAttribute("equipCategoryMapper", equipCategoryFormMaps);
		return Common.BACKGROUND_PATH + "/maintain/maintain_info";
	}
	
	@ResponseBody
	@RequestMapping("maintain_findByPage")
	public PageView maintain_findByPage(String pageNow, String pageSize) throws Exception 
	{
		RepairappFormMap repairappFormMap=getFormMap(RepairappFormMap.class);
		List<Map<String, Object>> repairappFormMaps=repairappMapper.getAllRepairs(repairappFormMap);
		for (Map<String, Object> map : repairappFormMaps) {
			if(Integer.valueOf(map.get("formCategoryId").toString())==2){
				EquipFileFormMap equipFileFormMap=equipFileMapper.findbyFrist("id", map.get("fileId").toString(), EquipFileFormMap.class);
				map.put("equipname", equipFileFormMap.get("equipmentNo"));
			}
		}
		LuogicPaging(pageNow, pageSize, repairappFormMaps);
		return pageView;
	}
	
	@RequestMapping("maintain_info_add")
	@ResponseBody
	public String maintain_info_add(Model model){
		RepairappFormMap repairappFormMap=getFormMap(RepairappFormMap.class);
		//添加申请人
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		StaffFormMap staffFormMap=staffMapper.findbyFrist("user_id", userFormMap.get("id").toString(), StaffFormMap.class);
		repairappFormMap.put("applyPeopleId", staffFormMap.get("id"));
		if(Integer.valueOf(repairappFormMap.get("formCategoryId").toString())==1){
			//模具
			
		}else if(Integer.valueOf(repairappFormMap.get("formCategoryId").toString())==2){
			//设备
			EquipFileFormMap equipFileFormMap=equipFileMapper.findbyFrist("equipmentNo", repairappFormMap.get("equipmentNo").toString(), EquipFileFormMap.class);
			repairappFormMap.put("fileId", equipFileFormMap.get("id"));
		}
		try {
			repairappFormMap.put("status", 1);
			repairappMapper.addEntity(repairappFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	@RequestMapping("shebeiIfExict")
	@ResponseBody
	public String shebeiIfExict(String shebei){
		List<EquipFileFormMap> equipFileFormMap=equipFileMapper.findByAttribute("equipmentNo", shebei, EquipFileFormMap.class);
		if(equipFileFormMap.size()>0){
			return "true";
		}else{
			return "false";
		}
	}
	
	@RequestMapping("moldIfExict")
	@ResponseBody
	public String moldIfExict(String mold){
		List<MainMouldFileFormMap> equipFileFormMap=equipFileMapper.findByAttribute("mouldNo", mold, MainMouldFileFormMap.class);
		if(equipFileFormMap.size()>0){
			return "true";
		}else{
			return "false";
		}
	}
}
